{include file='public/head'}
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="/static/layui/dist/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>

<script>
    layui.use('table', function(){
        var table = layui.table;
        table.render({
            elem: '#test'
            ,url:'/admin/keyword/getKeywords?page=1&limit=10'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,title: '用户分配表'
            ,cols: [[{templet: '#indexTpl', title:'序号', fixed: 'left', unresize: true,align:'center'}
                ,{field:'keyword', title:'关键字', edit: 'text',align:'center'}
                ,{field:'id', title:'id',hide:true, edit: 'text',align:'center'}
                ,{field:'add_user_name', title:'添加人',edit: 'text',align:'center'}
                ,{field:'add_user_id', title:'添加人id',hide:true, edit: 'text', sort: true,align:'center'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo',align:'center',minWidth:300}
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    x_admin_show('新增敏感词','goAddKeyword',500,410);
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么?', function(index){
                    $.ajax({
                        url:"delKeyword?id="+data.id,
                        method:"get",
                        success:function (data) {
                            obj.del();
                            layer.close(index);
                            window.location.reload();
                        }
                    })

                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.keyword
                }, function(value, index){

                    $.ajax({
                        url:"updateKeyword?id="+data.id+"&keyword="+value,
                        method:"get",
                        success:function (data) {
                            layer.alert(data.data);
                            obj.update({
                                keyword: value
                            });
                            window.location.reload();
                        }
                    })
                });
            }
        });
    });
</script>
<script type="text/html" id="indexTpl">
    {{d.LAY_TABLE_INDEX+1}}
</script>
</body>
</html>